package com.iraeta.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.common.AuthAccess;
import com.iraeta.common.ServiceException;
import com.iraeta.entity.Yijianbox;
import com.iraeta.entity.Yijiantype;
import com.iraeta.service.YiJianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController(value = "YiJianController")
@RequestMapping("yijian")
public class YiJianController extends BaseController{
    @Autowired
    YiJianService yiJianService;

    /*
     * 意见分类功能
     * */
    @AuthAccess
    @RequestMapping("pageyijiantype")
    public String pageYiJianType(){
        List<Yijiantype> yijiantypeList = yiJianService.selectYijiantypeList();
        return this.success(yijiantypeList);
    }

    @RequestMapping("getyijiantype")
    public String getYiJianType(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        Yijiantype yijiantype = yiJianService.getYiJianType(id);
        return this.success(yijiantype);
    }

    @RequestMapping("addedityijiantype")
    public String addYiJianType(@RequestBody JSONObject paramJb) {
        int id = paramJb.getIntValue("id");
        Yijiantype yijiantype = id <= 0 ? new Yijiantype() :  yiJianService.getYiJianType(id);
        this.mapJsonToObj(paramJb, yijiantype);
        if(id <= 0) yijiantype.setId(0);
        if(id <= 0){
            yiJianService.addYiJianType(yijiantype);
        }else {
            yiJianService.updateYiJianType(yijiantype);
        }
        return this.success();
    }

    @RequestMapping("deleteyijiantype")
    public String deleteYiJianType(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        yiJianService.deleteYiJianType(id);
        return this.success();
    }

    /*
    * 意见箱功能
    * */
    @RequestMapping("pageyijianbox")
    public String pageYiJianBox(@RequestBody JSONObject jo){
        String startDate = jo.getString("startDate");
        String endDate = jo.getString("endDate") == null ? null : DateUtil.formatDate( DateUtil.parseDate(jo.getString("endDate") ).offset(DateField.DAY_OF_MONTH, 1));
        Integer typeId = jo.getInteger("typeId");
        this.pageSet(jo);
        List<Yijianbox> yijianboxList = yiJianService.selectYijianboxList(typeId, startDate, endDate);
        PageInfo<Yijianbox> yijianboxPageInfo = this.getPageInfo(yijianboxList);
        return this.success(yijianboxPageInfo);
    }

    @RequestMapping("getyijianbox")
    public String getYiJianBox(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        Yijianbox yijianbox = yiJianService.getYiJianBox(id);
        return this.success(yijianbox);
    }

    @AuthAccess
    @RequestMapping("addedityijianbox")
    public String addYiJianBox(@RequestBody JSONObject paramJb) {
        String uuid = paramJb.getString("uuid");
        String token = paramJb.getString("token");
        if(StrUtil.isEmpty(uuid) || uuid.length() <=20){
            throw new ServiceException("error add, please check");
        }
        if(StrUtil.isEmpty(token) || !token.startsWith("iraeta")){
            throw new ServiceException("error add, please check");
        }
        int id = paramJb.getIntValue("id");
        Yijianbox yijianbox = id <= 0 ? new Yijianbox() :  yiJianService.getYiJianBox(id);
        this.mapJsonToObj(paramJb, yijianbox);
        if(id <= 0) yijianbox.setId(0);
        if(id <= 0){
            yijianbox.setCreatetime(new Date());
            yiJianService.addYiJianBox(yijianbox);
        }else {
            yiJianService.updateYiJianBox(yijianbox);
        }
        return this.success();
    }

    @RequestMapping("deleteyijianbox")
    public String deleteYiJianBox(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        yiJianService.deleteYiJianBox(id);
        return this.success();
    }
}
